import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

import path,{ resolve } from 'node:path'

const host = process.env.TAURI_DEV_HOST;

console.log(host)
// https://vite.dev/config/
export default defineConfig(({ mode, command }) => {
  return {

    resolve: {
      alias: {
        "~": resolve(__dirname, "./"),
        "@": resolve(__dirname, "./src"), // 假设你的源码目录是src
      },
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
    },
    plugins: [vue()],
    clearScreen: false,
    server: {
      host: host||false,
      strictPort: true,
      port: 1420,
      cors: true,
      hmr: host
      ? {
          protocol: "ws",
          host,
          port: 1421,
        }
      : undefined,
      watch: {
        // 3. tell vite to ignore watching `src-tauri`
        ignored: ["**/src-tauri/**"],
      },
      proxy: {
        "/dev-api": {
          target: "http://192.168.100.223:86",
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api/, "/api"),
        },
      },
    },
    // 添加有关当前构建目标的额外前缀，使这些 CLI 设置的 Tauri 环境变量可以在客户端代码中访问
    envPrefix: [
      "VITE_",
      "NODE_*",
      "TAURI_ENV_*",
      "TAURI_PLATFORM",
      "TAURI_ARCH",
      "TAURI_FAMILY",
      "TAURI_PLATFORM_VERSION",
      "TAURI_PLATFORM_TYPE",
      "TAURI_DEBUG",
    ],
    build: {
      emptyOutDir: false,
      // Tauri 在 Windows 上使用 Chromium，在 macOS 和 Linux 上使用 WebKit
      target:  process.env.TAURI_ENV_PLATFORM == "windows" ? "chrome105" : "safari13",
      // 在 debug 构建中不使用 minify
      minify: !process.env.TAURI_ENV_DEBUG ? "esbuild" : false,
      // 在 debug 构建中生成 sourcemap
      sourcemap: !!process.env.TAURI_ENV_DEBUG,
    },
  }
})
